package com.demo.springsecuritystudy.service;

import com.demo.springsecuritystudy.entiry.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService implements UserDetailsService {

    @Value("${application.customize.username:admin}")
    private String customizeUsername;

    @Value("${application.customize.password:123}")
    private String customizePassword;

    @Value("${application.customize.permission.list:admin,normal,ROLE_hello,/main.html}")
    private String permissionList;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return new User(customizeUsername, passwordEncoder.encode(customizePassword), AuthorityUtils.commaSeparatedStringToAuthorityList(permissionList));
    }
}
